package org.cloudnote.service.impl;

import org.cloudnote.dao.NoteDao;
import org.cloudnote.entity.Note;
import org.cloudnote.service.NoteService;
import org.cloudnote.util.NoteResult;
import org.cloudnote.util.NoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteDao noteDao;

    @Override
    public NoteResult updateNote(String noteTitle, String noteBody, String noteId) {
        Note note = new Note();
        note.setCn_note_id(noteId);
        note.setCn_note_title(noteTitle);
        note.setCn_note_body(noteBody);
        long time =System.currentTimeMillis();
        note.setCn_note_last_modify_time(time);
        noteDao.update(note);
        NoteResult result = new NoteResult();
        result.setStatus(0);
        result.setMsg("保存笔记成功");
        return result;
    }

    @Override
    public NoteResult addNote(String bookId, String userId, String noteTile) {
        Note note = new Note();
        note.setCn_notebook_id(bookId);
        note.setCn_user_id(userId);
        note.setCn_note_title(noteTile);
        String noteId = NoteUtil.createdId();
        note.setCn_note_id(noteId);
        note.setCn_note_status_id("1");
        note.setCn_note_type_id("1");
        note.setCn_note_body("");
        long time = System.currentTimeMillis();
        note.setCn_note_create_time(time);
        note.setCn_note_last_modify_time(time);

        noteDao.save(note);

        NoteResult result = new NoteResult();

        result.setStatus(0);
        result.setData(noteId);
        result.setMsg("创建笔记成功");
        return result;
    }

    @Override
    public NoteResult loadNoteDetail(String id) {
        NoteResult result = new NoteResult();

        Note note = noteDao.findById(id);
        if(note == null){
            result.setStatus(1);
            result.setMsg("找不到笔记信息");
            return result;
        }
        result.setStatus(0);
        result.setMsg("查找笔记成功");
        result.setData(note);

        return result;
    }

    @Override
    public NoteResult loadNotes(String bookId) {
        NoteResult result = new NoteResult();
        List<Note> list = noteDao.findByBookId(bookId);
        result.setStatus(0);
        result.setMsg("加载笔记列表成功");
        result.setData(list);
        return result;
    }
}
